﻿using System;
using System.Collections.Generic;
using System.Text;
using DBScript;
using eBlog.Entity;
namespace eBlog.Service
{
    public class CommentService
    {

        /// <summary>
        /// 最近评论列表
        /// </summary>
        private static List<Comments> _recentcomments;

        /// <summary>
        /// lock
        /// </summary>
        private static object lockHelper = new object();

        static CommentService()
        {
               //LoadRecentComment(); 
        }


        /// <summary>
        /// 加载最近评论
        /// </summary>
        private static void LoadRecentComment(int rowCount)
        {
            lock (lockHelper)
            {
                _recentcomments = GetCommentList(rowCount, 1, -1, -1, 0, (int)ApprovedStatus.Success, -1, string.Empty);
            }
        }


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="comment"></param>
        /// <returns></returns>
        public static int InsertComment(Comments comment)
        {
            IDBBase dal = DBFactory.GetDbProvider();
            int result = dal.Add(comment);

            //统计
            StatisticsService.UpdateStatisticsCommentCount(1);

            ////用户
            //UserService.UpdateUserCommentCount(comment.UserId, 1);

            ////文章
            //PostsService.UpdatePostCommentCount(comment.PostId, 1);


            _recentcomments = null;

            return result;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="comment"></param>
        /// <returns></returns>
        public static bool UpdateComment(Comments comment)
        {
            IDBBase dal = DBFactory.GetDbProvider();
            _recentcomments = null;

            return dal.Update(comment);

        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="commentId"></param>
        /// <returns></returns>
        public static int DeleteComment(int commentId)
        {
            IDBBase dal = DBFactory.GetDbProvider();
            int result = dal.Delete<Comments>(commentId);

            //统计
            StatisticsService.UpdateStatisticsCommentCount(-1);

            //if (comment != null)
            //{
            //    //用户
            //    UserManager.UpdateUserCommentCount(comment.UserId, -1);
            //    //文章
            //    PostManager.UpdatePostCommentCount(comment.PostId, -1);
            //}

            _recentcomments = null;

            return result;
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="commentId"></param>
        /// <returns></returns>
        public static Comments GetComment(int commentId)
        {
            IDBBase dal = DBFactory.GetDbProvider();
            Comments comment = dal.GetModel<Comments>(commentId);
            return comment;
        }


        /// <summary>
        /// 获取评论
        /// </summary>
        /// <param name="rowCount"></param>
        /// <param name="userId"></param>
        /// <param name="postId"></param>
        /// <param name="parentId"></param>
        /// <param name="staus"></param>
        /// <param name="emailNotify"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public static List<Comments> GetCommentList(int rowCount, int order, int userId, int postId, int parentId, int staus, int emailNotify, string keyword)
        {
           
            int totalRecord = 0;
            return GetCommentList(rowCount, 1, out totalRecord, order, userId, postId, parentId, staus, emailNotify, keyword);
        }

        /// <summary>
        /// 最近评论
        /// </summary>
        /// <param name="rowCount"></param>
        /// <returns></returns>
        public static List<Comments> GetCommentListByRecent(int rowCount)
        {
            if (_recentcomments == null)
            {
                LoadRecentComment(rowCount);
            }
            return _recentcomments;

            //string cacheKey = "/comment/recent";// +rowCount;

            //List<CommentInfo> list = (List<CommentInfo>)Caches.Get(cacheKey);
            //if (list == null)
            //{
            //    int totalRecord = 0;
            //    list = GetCommentList(rowCount, 1, out totalRecord, 1, -1, -1, 0, (int)stausStatus.Success, -1, string.Empty);

            //    Caches.Add(cacheKey, list);
            //}
            //return list;
        }

        /// <summary>
        /// 获取评论
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="totalRecord"></param>
        /// <param name="order"></param>
        /// <param name="userId"></param>
        /// <param name="postId"></param>
        /// <param name="parentId"></param>
        /// <param name="Staus"></param>
        /// <param name="emailNotify"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public static List<Comments> GetCommentList(int pageSize, int pageIndex, out int totalRecord, int order, int userId, int postId, int parentId, int Staus, int emailNotify, string keyword)
        {
            string condition = " 1=1 ";// "[ParentId]=0 and [PostId]=" + postId;

            if (userId != -1)
            {
                condition += " and userid=" + userId;
            }
            if (postId != -1)
            {
                condition += " and TargetID=" + postId;
            }
            if (parentId != -1)
            {
                condition += " and parentId=" + parentId;
            }

            if (Staus != -1)
            {
                condition += " and Staus=" + Staus;
            }

            if (emailNotify != -1)
            {
                condition += " and emailNotify=" + emailNotify;
            }

            if (!string.IsNullOrEmpty(keyword))
            {
                condition += string.Format(" and (content like '%{0}%' or author like '%{0}%' or ipaddress like '%{0}%' or email like '%{0}%'  or siteurl like '%{0}%' )", keyword);
            }

            IDBBase dal = DBFactory.GetDbProvider();
            List<Comments> list = dal.Where(condition).Page(pageIndex,pageSize).GetList<Comments>();
            totalRecord = list.Count;

            return list;
        }

        /// <summary>
        /// 根据日志ID删除评论
        /// </summary>
        /// <param name="postId">日志ID</param>
        /// <returns></returns>
        public static int DeleteCommentByPost(int postId)
        {
            IDBBase dal = DBFactory.GetDbProvider();
            int result = dal.Where("TargetID=@tid").SetInt("tid",postId).Delete();

            StatisticsService.UpdateStatisticsCommentCount(-result);

            _recentcomments = null;

            return result;
        }

        /// <summary>
        /// 统计评论数
        /// </summary>
        /// <param name="incChild"></param>
        /// <returns></returns>
        public static int GetCommentCount(bool incChild)
        {
            return GetCommentCount(-1, incChild);
        }

        /// <summary>
        /// 统计评论数
        /// </summary>
        /// <param name="postId"></param>
        /// <param name="incChild"></param>
        /// <returns></returns>
        public static int GetCommentCount(int postId, bool incChild)
        {
            return GetCommentCount(-1, postId, incChild);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="postId"></param>
        /// <param name="incChild"></param>
        /// <returns></returns>
        public static int GetCommentCount(int userId, int postId, bool incChild)
        {
            string condition = " 1=1 ";
            if (userId != -1)
            {
                condition += " and [UserID] = " + userId;
            }
            if (postId != -1)
            {
                condition += " and [TargetID] = " + postId;
            }
            if (incChild == false)
            {
                condition += " and [ParentID]=0";
            }
            IDBBase dal = DBFactory.GetDbProvider();
            return dal.From("Comments","ID").Where(condition).GetCount();
        }

    }
}
